<template>
  <div class="articaldetail">
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back" @click="$router.back()" />
        <span class="iconfont iconnew new"></span>
      </div>
      <span @click="followFn" :class="{ active: obj.has_follow }">{{
        obj.has_follow ? "已关注" : "关注"
      }}</span>
    </div>
    <div class="detail">
      <div class="title">{{ obj.title }}</div>
      <div class="desc">
        <span>{{ obj.user && obj.user.nickname }}</span> &nbsp;&nbsp;
        <span>{{ new Date() | printfTime }}</span>
      </div>
      <div v-if="obj.type == 1" class="content" v-html="obj.content"></div>
      <div v-if="obj.type == 2" class="content">
        <video :src="obj.content" controls autoplay></video>
      </div>
      <div class="opt">
        <span class="like" @click="likeFn">
          <van-icon :class="{ active: obj.has_like }" name="good-job-o" />{{
            this.obj.has_like >= 0 ? obj.like_length : "点赞"
          }}
        </span>
        <span class="chat" @click="showShare = true">
          <van-icon name="chat" class="w" />微信
        </span>
        <van-share-sheet
          v-model="showShare"
          title="立即分享给好友"
          :options="options"
          @select="onSelect"
        />
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <div class="item">
        <div class="head">
          <img src="" alt />
          <div>
            <p>火星网友</p>
            <span>2小时前</span>
          </div>
          <span>回复</span>
        </div>
        <div class="text">文章说得很有道理</div>
      </div>
      <div class="more">更多跟帖</div>
    </div>
    <footComment :obj="obj"></footComment>
  </div>
</template>

<script>
import footComment from "@/components/footComment.vue";
import { newsDetails, userFollows, userUnfollows, like } from "@/apis/get";
export default {
  data() {
    return {
      obj: {},
      has_like: false,
      showShare: false,
      options: [
        { name: "微信", icon: "wechat" },
        { name: "微博", icon: "weibo" },
        { name: "复制链接", icon: "link" },
        { name: "分享海报", icon: "poster" },
        { name: "二维码", icon: "qrcode" },
      ],
    };
  },
  components: {
    footComment,
  },
  created() {
    // console.log(this.$route.query.id);
    // 新闻详细信息
    newsDetails(this.$route.query.id).then((res) => {
      // console.log(res);
      this.obj = res.data.data;
      // console.log(this.obj);
    });
  },
  methods: {
    // 关注与取消关注
    followFn() {
      if (this.obj.has_follow) {
        // 已关注，点击取消关注
        this.followToggle(userUnfollows, this.obj.user.id, "取消关注成功");
      } else {
        // 未关注，点击关注
        this.followToggle(userFollows, this.obj.user.id, "关注成功");
      }
    },
    // 关注函数封装
    followToggle(backFn, id, val) {
      backFn(id).then((res) => {
        // console.log(79, res);
        if (res.data.message == val) {
          this.$toast(res.data.message);
        }
        this.obj.has_follow = !this.obj.has_follow;
      });
    },
    // 点赞
    likeFn() {
      like(this.obj.id).then((res) => {
        // console.log(118, res);
        this.obj.has_like = !this.obj.has_like;
        this.obj.has_like ? this.obj.like_length++ : this.obj.like_length--;
        this.$toast(res.data.message);
      });
    },
    // 分享
    onSelect(option) {
      this.$toast(option.name);
      this.showShare = false;
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
    &.active {
      background-color: #fff;
      color: #000;
      border: 1px solid rgb(115, 115, 115);
    }
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    /deep/img {
      width: 90%;
    }
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .like {
    .active {
      color: red;
    }
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  margin-bottom: 100px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: (50/360) * 100vw;
        height: (50/360) * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>